package com.woniu.market_stall.web;

import com.github.pagehelper.PageInfo;
import com.woniu.market_stall.entity.Stall;
import com.woniu.market_stall.result.ResultResponse;
import com.woniu.market_stall.service.StallService;
import com.woniu.market_stall.vo.ManagerVo;
import com.woniu.market_stall.vo.StallVo;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.util.List;
import java.util.UUID;

@RestController
@RequestMapping("stall")
public class StallController {

    @Autowired
    StallService stallService;

    @Value("${imgPath}")
    private String imgPath;


    @GetMapping("/list")
    @ApiOperation(value = "本接口是查询按条件分页查询摊位数据的")
    public ResultResponse<PageInfo<StallVo>> list(
            @ApiParam(value = "当前页的参数") @RequestParam(value = "now", defaultValue = "1") Integer now,
            @ApiParam(value = "页面大小的参数") @RequestParam(value = "size", defaultValue = "3") Integer size,
            @ApiParam(value = "按条件搜索的摊位名称") @RequestParam(value = "stallName", defaultValue = "") String stallName,
            @ApiParam(value = "按条件搜索的摊位状态") @RequestParam(value = "status", defaultValue = "") String status,
            @ApiParam(value = "按条件搜索的市场的id") @RequestParam(value = "marketId", defaultValue = "0") String marketId) {
        ResultResponse<PageInfo<StallVo>> result = null;
        try {
            PageInfo<StallVo> pageInfo = stallService.queryEmpByPage(now, size, status, marketId, stallName);
            result = new ResultResponse<>(200, "查询摊位成功!", pageInfo);
        } catch (RuntimeException e) {
            e.printStackTrace();
            result = new ResultResponse<>(201, "查询摊位失败!");
        }
        return result;
    }


    @PostMapping("/add/{managerId}")
    @ApiOperation(value = "本接口是添加新的摊位的")
    public ResultResponse<Void> add(@RequestBody StallVo stallVo , @PathVariable("managerId") int managerId ) {
        ResultResponse<Void> result = null;
        try {
            stallService.add(stallVo , managerId );
            result = new ResultResponse<>(200, "添加摊位成功");
        } catch (RuntimeException e) {
            e.printStackTrace();
            result = new ResultResponse<>(201, "添加摊位失败");
        }
        return result;
    }


    @PostMapping("/uploadImg")
    public ResultResponse<String> uploadImg(
            @RequestParam(value = "uploadImg") MultipartFile multipartFile) {
        System.out.println("multipartFile" + multipartFile);
        ResultResponse<String> result = null;
        try {
            //获取 上传文件的名字
            String filename = multipartFile.getOriginalFilename();
            //重新定义上传文件名字
            filename = UUID.randomUUID().toString().replace("-", "") + filename.substring(filename.lastIndexOf("."));

            File f = new File(imgPath);
            if (f.exists()) {
                f.mkdirs();
            }

            File file = new File(f, filename);

            //上传
            multipartFile.transferTo(file);
            result = new ResultResponse<>(200, "上传成功", filename);


        } catch (Exception e) {
            e.printStackTrace();
            result = new ResultResponse<>(201, "上传失败");
        }
        return result;
    }

    /**
     * 修改 摊位信息
     *
     * @param stallVo
     * @return
     */
    @PutMapping("/modify/{managerId}")
    @ApiOperation(value = "本接口是修改摊位")
    public ResultResponse<Void> modify(@RequestBody StallVo stallVo ,  @PathVariable("managerId") int managerId) {
        ResultResponse<Void> result = null;
        try {
            stallService.modify(stallVo , managerId);
            result = new ResultResponse<>(200, "修改摊位成功!");

        } catch (RuntimeException e) {
            e.printStackTrace();
            result = new ResultResponse<>(201, "修改摊位失败!");
        }
        return result;
    }


    /**
     * 删除 摊位信息
     * @param id 需要删除的id
     * @return
     */
    @PutMapping("/del/{id}")
    @ApiOperation(value = "本接口是删除摊位")
    public ResultResponse<Void> del(@PathVariable("id") int id) {
        ResultResponse<Void> result = null;
        try {
            int i = stallService.del(id);
            if(i == 0){
                result = new ResultResponse<>(200, "删除摊位成功!");
            }else{
                result = new ResultResponse<>(202, "删除失败，使用中的摊位不可删除!");
            }

        } catch (RuntimeException e) {
            e.printStackTrace();
            result = new ResultResponse<>(201, "删除摊位失败!");
        }
        return result;
    }


    /**
     * 查询所有未使用的摊位
     *
     * @return
     */
    @GetMapping("/showAllUnUsedStall")
    @ApiOperation(value = "本接口是查询所有未使用的摊位数据")
    public ResultResponse<List<Stall>> showAllUnUsedStall() {
        ResultResponse<List<Stall>> result = null;
        try {
            List<Stall> stalls = stallService.queryAllUnUsedStall();
            result = new ResultResponse<>(200, "查询未使用摊位成功!", stalls);
        } catch (RuntimeException e) {
            e.printStackTrace();
            result = new ResultResponse<>(201, "查询未使用摊位失败!");
        }
        return result;
    }


    /**
     * 查询所有竞拍中的摊位数据
     *
     * @return
     */
    @GetMapping("/showAllAuctioningStall")
    @ApiOperation(value = "本接口是查询所有竞拍中的摊位数据")
    public ResultResponse<List<Stall>> showAllAuctioningStall() {
        ResultResponse<List<Stall>> result = null;
        try {
            List<Stall> stalls = stallService.queryAllAuctioningStall();
            result = new ResultResponse<>(200, "查询所有竞拍摊位成功!", stalls);
        } catch (RuntimeException e) {
            e.printStackTrace();
            result = new ResultResponse<>(201, "查询所有竞拍摊位失败!");
        }
        return result;
    }


    /**
     * 查询所有摊位
     *
     * @return
     */
    @GetMapping("/showAllStall")
    @ApiOperation(value = "本接口是查询所有未使用的摊位数据")
    public ResultResponse<List<Stall>> showAllStall() {
        ResultResponse<List<Stall>> result = null;
        try {
            List<Stall> stalls = stallService.queryAllStall();
            result = new ResultResponse<>(200, "查询所有的摊位成功!", stalls);
        } catch (RuntimeException e) {
            e.printStackTrace();
            result = new ResultResponse<>(201, "查询所有的摊位失败!");
        }
        return result;
    }
}
